package edu.uow.devdocs

class AssignmentController {
    static scaffold = true
	def currentUserService
	def assignmentService
	def emailService
	
	def addAssignment() {
		def assignmentInstance = assignmentService.createDefaultAssignment()
		bindData(assignmentInstance, params)
		if (assignmentInstance.validate()) {
			assignmentInstance.save(flush:true)
			flash.message = message(code: 'default.created.message', args: [message(code: 'Assignment.label', default: 'Assignment'), assignmentInstance.id])
			assignmentService.addToProject(assignmentInstance.person.id,assignmentInstance.project.id, assignmentInstance)
			emailService.notifyNewAssignment(assignmentInstance)
		}
		render(template:"/shared/addAssignment", 
			model:['assignmentInstance':assignmentInstance.hasErrors()?assignmentInstance:new Assignment(),
			'issueId':assignmentInstance?.issue?.id,
			'developmentChecklistId':assignmentInstance?.developmentChecklist?.id,
			'developmentTaskId': assignmentInstance?.developmentTask?.id,
			'deploymentId': assignmentInstance?.deployment?.id])
	}
	
	def list() {
		println AssignmentStatus.getOpenStatus()
		println AssignmentStatus.getOpenStatus().id
		redirect (action: 'search', params: ['person.id':currentUserService.currentUserId,'status.id': AssignmentStatus.getOpenStatus().id])
	}
	
	def remoteSave(int id) {
		def assignmentInstance = Assignment.get(id)
		def statusInstance = AssignmentStatus.get(params?.status.id)
		assignmentInstance.status = statusInstance
		assignmentInstance?.save()
		flash.message = message(code: 'default.updated.message', args: [message(code: 'Assignment.label', default: 'Assignment'), assignmentInstance.id])
		render( template:"/shared/assignment", model:[assignmentInstance:assignmentInstance]) 
	}
	
}
